import React, { Component } from 'react';
import './index.css';
import { nanoid } from 'nanoid';

export default class HeaderInp extends Component {
  state = {
    textTitle: ''
  };
  handleTitle = (ev) => {
    this.setState({
      textTitle: ev.target.value
    });
  };
  addTodo = (ev) => {
    if (ev.keyCode === 13) {
      this.props.addTodo({
        id: nanoid(),
        title: ev.target.value,
        done: false
      });
      this.setState({
        textTitle: ''
      });
    }
  };

  render() {
    return (
      <div className="todo-header">
        <input
          value={this.state.textTitle}
          onKeyDown={this.addTodo}
          onChange={this.handleTitle}
          type="text"
          placeholder="请输入你的任务名称，按回车键确认"
        />
      </div>
    );
  }
}
